package com.carpool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import com.carpool.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


public class MyFullContactActivity extends ListActivity {
	final int PICK_CONTACT = 0;
	int counter;

	private SimpleAdapter mAdapter;
	private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
	private ArrayList<String> addresses = new ArrayList<String>();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		counter = 0;

		Bundle bundle = getIntent().getExtras();
		addresses = bundle.getStringArrayList("addresses");

		mAdapter = new SimpleAdapter(this, list, R.layout.contacts, new String[] { "line1","line2" }, new int[] { R.id.text1, R.id.text2 });
		setListAdapter(mAdapter);

		Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null , null, null, null); 
		while (cursor.moveToNext()) { 
			String address = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
			String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME));
			addItem(name, address);
		}
		cursor.close();

	}

	private void addItem(String name, String address) {
		HashMap<String,String> item = new HashMap<String,String>();
		item.put("line1", name);
		item.put("line2", address);
		list.add(item);
		mAdapter.notifyDataSetChanged();

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(getApplicationContext(), MyMapActivity.class);

		@SuppressWarnings("unchecked")
		HashMap<String,String> result = (HashMap<String,String>)l.getItemAtPosition(position);
		@SuppressWarnings("rawtypes")
		Collection c = result.values();
		@SuppressWarnings("rawtypes")
		Iterator itr = c.iterator();

		itr.next().toString();
		String address = itr.next().toString();
		Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
		try {
			List<Address> addresses = geoCoder.getFromLocationName(address, 1);
			if (addresses.size() > 0) 
			{
				this.addresses.add(address);
				Bundle bundle = new Bundle();
				bundle.putStringArrayList("addresses", this.addresses);
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
			else {
				Toast.makeText(getBaseContext(), "Contact has invalid address.", Toast.LENGTH_LONG).show();
			}
		} catch (IOException e) {          
		} catch (NullPointerException e) {
		};

	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, MyStep3Activity.class);
		Bundle bundle = new Bundle();
		bundle.putStringArrayList("addresses", this.addresses);
		intent.putExtras(bundle);
		startActivity(intent);
		finish();
		return;

	}

}